﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

namespace QuangMobile.Admin
{
    public partial class NhomHienThi : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadListBanner();
            }
        }

        protected void LoadListBanner()
        {
            string sql = "SELECT * FROM dbo.tblWebDanhMuc where MaDM = 'nhom'";
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql);
            grvData.DataSource = dt;
            grvData.DataBind();

        }

        protected void SuaNhom(int IsShow, int ID)
        {
            string sql = "UPDATE dbo.tblWebDanhMuc SET IsShow = @IsShow,UserID = @UserID,UpdateDate = GETDATE() WHERE ID = @ID";

            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@IsShow", SqlDbType.Int, ParameterDirection.Input, IsShow));
            arrParams.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Session["UserID"]));
            arrParams.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, ID));
            SqlHelper.ExecuteNonQuery(CommandType.Text, sql, arrParams);
        }

        protected void grvData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string sID = e.CommandArgument.ToString().Trim();
            string sCommandName = e.CommandName;

            if ((sCommandName == QuangMobile.MyConst.Action.Approve))
            {
                List<SqlParameter> arrParams = new List<SqlParameter>();
                RadioButton rbtSuDung = (RadioButton)grvData.Rows[grvData.EditIndex].Cells[1].Controls[1];
                SuaNhom(rbtSuDung.Checked ? 1 : 0, Convert.ToInt32(sID));
            }
            if ((sCommandName == QuangMobile.MyConst.Action.Delete))
            {
                string sql = "DELETE FROM dbo.tblWebDanhMuc WHERE ID = " + sID;
                SqlHelper.ExecuteNonQuery(CommandType.Text, sql);
            }
            grvData.EditIndex = -1;
            LoadListBanner();
        }

        protected void grvData_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grvData.EditIndex = e.NewEditIndex;
            LoadListBanner();
        }

        protected void grvData_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grvData.EditIndex = -1;
            LoadListBanner();
        }

        protected void grvData_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            grvData.DeleteRow(e.RowIndex);
            grvData.DataBind();
        }
    }
}
